using System.Linq;

namespace TestEngine
{
    public class OpenQuestionState : IQuestionState
    {
        public int[] AnswersVariants { get; private set; }
        public OpenQuestion Question { get; private set; }

        public OpenQuestionState(OpenQuestion question, params int[] variants)
        {
            Question = question;
            AnswersVariants = variants ?? new int[0];
        }

        public double Score()
        {
            var correctCount = 0;
            foreach (var answer in AnswersVariants)
            {
                if (Question.Variants[answer].IsCorrect) correctCount++;
                else correctCount--;
            }
            double factor = 0.0;
            if (correctCount > 0)
                factor = correctCount/(double) Question.Variants.Count(v => v.IsCorrect);
            return factor*Question.Cost;
        }

        public string[] GetAnswers()
        {
            var res = new string[AnswersVariants.Length];
            for (int i = 0; i < AnswersVariants.Length; ++i)
                res[i] = Question.Variants[i].ID.ToString();
            return res;
        }
    }
}